/** 
* 文 件 名:SportManageLogic.java
* Copyright (C) 2007-2012:杭州希禾信息技术有限公司 版权所有 
* 创 建 人:刘绍辉
* 日    期:2012-3-12 下午3:36:17
* 修 改 人: 
* 日   期:
* 修改描述:
* 描   述:TODO
*/
package net.seehealth.healthpad.business.sportmanage;

import java.util.ArrayList;
import java.util.List;

import net.seehealth.healthpad.application.base.BaseLogic;
import net.seehealth.healthpad.application.base.BaseModel;
import net.seehealth.healthpad.business.sportmanage.service.ISportManageLogic;
import net.seehealth.healthpad.dataaccess.HealthSqliteHelper;
import net.seehealth.healthpad.model.foodmanage.Food;
import net.seehealth.healthpad.model.sportmanage.SportGuide;
import net.seehealth.healthpad.model.sportmanage.SportItem;
import net.seehealth.healthpad.model.sportmanage.SportType;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


/**
 * @className SportManageLogic
 * @author 刘绍辉
 * @description TODO 运动管理业务类
 * @date 2012-3-12 下午5:22:54
 */
public class SportManageLogic extends BaseLogic implements ISportManageLogic{
	private HealthSqliteHelper helper = null;
	public final static String TAG = "SportManageLogic";
	public SportManageLogic (Context context){
		helper = new HealthSqliteHelper(context);
	}
	@Override
	public SportGuide getLocalSportGuide(int userId) {
		SportGuide sportGuide = new SportGuide();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.query("sportguide", null, "userID = ?", new String []{userId+""}, null, null, null);
		if(cursor.moveToFirst()){
			sportGuide.setSid(cursor.getInt(cursor.getColumnIndex("fid")));
			sportGuide.setUserID(cursor.getInt(cursor.getColumnIndex("userID")));
			sportGuide.setBmi(cursor.getFloat(cursor.getColumnIndex("bmi")));
			sportGuide.setNowWeight(cursor.getFloat(cursor.getColumnIndex("nowWeight")));
			sportGuide.setIdeaWeight(cursor.getFloat(cursor.getColumnIndex("ideaWeight")));
			sportGuide.setTodaySport(cursor.getString(cursor.getColumnIndex("todaySport")));
			sportGuide.setSportAdvice(cursor.getString(cursor.getColumnIndex("sportAdvice")));
			sportGuide.setSportTaboo(cursor.getString(cursor.getColumnIndex("sportTaboo")));
		}
		cursor.close();
		db.close();
		return sportGuide;
	}
	@Override
	public boolean saveLocalSportGuide(SportGuide sportGuide) {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues  values = new ContentValues();
		values.put("userID", sportGuide.getUserID());
		values.put("bmi", sportGuide.getBmi());
		values.put("nowWeight", sportGuide.getNowWeight());
		values.put("ideaWeight", sportGuide.getIdeaWeight());
		values.put("todaySport", sportGuide.getTodaySport());
		values.put("sportAdvice", sportGuide.getSportAdvice());
		values.put("sportTaboo", sportGuide.getSportTaboo());
		long num = db.insert("sportguide", null, values);
		if(num!=0){
			bool = true;
		}
		return bool;
	}
	@Override
	public void removeLocalSportGuide(int userID) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.delete("sportguide", "userID=?", new String []{userID+""});
		db.close();
	}
	/**
	 * 获取所有运动项目
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 上午11:57:51
	 */
	@Override
	public List<SportItem> getLocalSportItems() {
		List<SportItem> sportItems = new ArrayList<SportItem>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.query("sportitem", null, null, null, null, null, null);
		for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
			SportItem sportItem = new SportItem();
			sportItem.setSid(cursor.getInt(cursor.getColumnIndex("sid")));
			sportItem.setSportIntension(cursor.getInt(cursor.getColumnIndex("sportIntension")));
			sportItem.setSportName(cursor.getString(cursor.getColumnIndex("sportName")));
			sportItem.setMetValue(cursor.getFloat(cursor.getColumnIndex("metValue")));
			sportItem.setSportNum(cursor.getFloat(cursor.getColumnIndex("sportNum")));
			sportItem.setSportTime(cursor.getInt(cursor.getColumnIndex("sportTime")));
			sportItem.setPng(cursor.getString(cursor.getColumnIndex("png")));
			sportItem.setSportAdvice(cursor.getString(cursor.getColumnIndex("sportAdvice")));
			sportItem.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
			sportItem.setSportTypeCode(cursor.getString(cursor.getColumnIndex("sportTypeCode")));
			sportItems.add(sportItem);
		}
		cursor.close();
		db.close();
		return sportItems;
	}
	/**
	 * 获取所有运动类型
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 上午11:58:11
	 */
	@Override
	public List<SportType> getLocalSportTypes() {
		List<SportType> sportTypes = new ArrayList<SportType>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.query("sportitem", null, null, null, null, null, null);
		for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
			SportType sportType = new SportType();
			sportType.setStid(cursor.getInt(cursor.getColumnIndex("stid")));
			sportType.setTypeName(cursor.getString(cursor.getColumnIndex("typeName")));
			sportType.setSportTypeCode(cursor.getString(cursor.getColumnIndex("sportTypeCode")));
			sportTypes.add(sportType);
		}
		cursor.close();
		db.close();
		return sportTypes;
	}
	@Override
	public boolean saveLocalSportItem(SportItem sportItem) {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues  values = new ContentValues();
		values.put("sportItemNo", sportItem.getSportItemNo());
		values.put("sportIntension", sportItem.getSportIntension());
		values.put("sportName", sportItem.getSportName());
		values.put("metValue", sportItem.getMetValue());
		values.put("sportNum", sportItem.getSportNum());
		values.put("sportTime", sportItem.getSportTime());
		values.put("png", sportItem.getPng());
		values.put("sportAdvice", sportItem.getSportAdvice());
		values.put("desc", sportItem.getDesc());
		values.put("sportTypeCode", sportItem.getSportTypeCode());
		long num = db.insert("sportitem", null, values);
		if(num!=0){
			bool = true;
		}
		return bool;
	}
	@Override
	public boolean saveLocalSportType(SportType sportType) {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues  values = new ContentValues();
		values.put("typeName", sportType.getTypeName());
		values.put("sportTypeCode", sportType.getSportTypeCode());
		long num = db.insert("sporttype", null, values);
		if(num!=0){
			bool = true;
		}
		return bool;
	}
	@Override
	public boolean removeLocalSportItems() {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		int num = db.delete("sportitem", null, null);
		if(num!=0){
			bool = true;
		}
		db.close();
		return bool;
	}
	@Override
	public boolean removeLocalSportTypes() {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		int num = db.delete("sporttype", null, null);
		if(num!=0){
			bool = true;
		}
		db.close();
		return bool;
	}
	@Override
	public BaseModel queryDataByUserId(int userId) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public BaseModel queryDataById(int pk) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean addData(BaseModel model) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean modifData(BaseModel model) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean modifData(ArrayList<BaseModel> datas) {
		// TODO Auto-generated method stub
		return false;
	}
}
